package com.bear.demo1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

import static java.lang.Thread.sleep;

/** Interrupted 为给一个线程设置一个打断标识
 * @author LiuShanshan
 * @version V1.0
 * @Description
 */
@Slf4j
public class InterruptedDemo1 {
    public static void main(String[] args) throws InterruptedException {
//        test1();
//        test2();
        test4();
    }
    
    /**
     * 不合理
     * @return
     * @author LiuShanshan
     */
    private static void test1() throws InterruptedException {
        Thread t1 = new Thread(()->{
            try {
                sleep(4000);
                System.out.println("休眠时间结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");
        t1.start();
//        sleep((long) 0.5);
        t1.interrupt();
        System.out.println("打断状态:" + t1.isInterrupted());
//        log.debug(" 打断状态: {}", t1.isInterrupted());
        System.out.println("程序还能正常运行");
    }

    /**
     * 正常打断
     * @return
     * @author LiuShanshan
     */
    private static void test2() throws InterruptedException {
        Thread t1 = new Thread(()->{
            while (true){
                Thread thread = Thread.currentThread();
                boolean interrupted = thread.isInterrupted();
                if(interrupted){
                    System.out.println("当前线程结束");
                    break;
                }
            }
        }, "t1");
        t1.start();
        Thread.sleep(2000);
        t1.interrupt();
        System.out.println("this{},正常打断");
    }

    /**
     * 还是interrupted中止堵塞状态，并且给该线程打上中断标记
     * @return
     * @author LiuShanshan
     */
    private static void test4() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                System.out.println("park...");
                LockSupport.park();
                System.out.println(Thread.currentThread().isInterrupted());
            }
        });
        t1.start();
        Thread.sleep(3000);
        t1.interrupt();
    }




}
